﻿Imports System.Reflection

<ComClass(ClsAcadDocument.ClassId, ClsAcadDocument.InterfaceId, ClsAcadDocument.EventsId)>
Public Class ClsAcadDocument

#Region "COM GUID"
    ' 这些 GUID 提供此类的 COM 标识 
    ' 及其 COM 接口。若更改它们，则现有的
    ' 客户端将不再能访问此类。
    Public Const ClassId As String = "bce088c2-82c4-45e1-b54f-ace1d3463625"
    Public Const InterfaceId As String = "dad45eb9-2e07-4189-aa43-f011ce3d0e3c"
    Public Const EventsId As String = "0d1ccc35-5da6-4725-845c-b49746a0ba00"
#End Region

    ' 可创建的 COM 类必须具有一个不带参数的 Public Sub New() 
    ' 否则， 将不会在 
    ' COM 注册表中注册此类，且无法通过
    ' CreateObject 创建此类。
    Public Sub New()
        MyBase.New()
    End Sub

    Private _ActiveLayer As String
    Public Property ActiveLayer() As String
        Get
            Return _ActiveLayer
        End Get
        Set(ByVal value As String)
            _ActiveLayer = value
        End Set
    End Property

    Private _fullname As String
    Public Property FullName() As String
        Get
            Return _fullname
        End Get
        Set(ByVal value As String)
            _fullname = value
        End Set
    End Property

    Private _name As String
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Private _path As String
    Public Property Path() As String
        Get
            Return _path
        End Get
        Set(ByVal value As String)
            _path = value
        End Set
    End Property


    Private _Saved As Boolean
    Public Property Saved() As Boolean
        Get
            Return _Saved
        End Get
        Set(ByVal value As Boolean)
            _Saved = value
        End Set
    End Property


    Private _ReadOnly As Boolean
    Public ReadOnly Property IsReadOnly() As Boolean
        Get
            Return Me.AcadObject.ReadOnly
        End Get

    End Property

    Private _Active As Boolean
    Public Property Active() As Boolean
        Get
            Return _Active
        End Get
        Set(ByVal value As Boolean)
            _Active = value
        End Set
    End Property

    Private _HWND As Long
    Public Property HWND() As Long
        Get
            Return _HWND
        End Get
        Set(ByVal value As Long)
            _HWND = value
        End Set
    End Property

    Private _WindowTitle As String
    Public Property WindowTitle() As String
        Get
            Return _WindowTitle
        End Get
        Set(ByVal value As String)
            _WindowTitle = value
        End Set
    End Property

    Private _ActiveLayout As String
    Public Property ActiveLayout() As String
        Get
            Return _ActiveLayout
        End Get
        Set(ByVal value As String)
            _ActiveLayout = value
        End Set
    End Property

    Private _ActiveLinetype As String
    Public Property ActiveLinetype() As String
        Get
            Return _ActiveLinetype
        End Get
        Set(ByVal value As String)
            _ActiveLinetype = value
        End Set
    End Property

    Private _ActiveDimStyle As String
    Public Property ActiveDimStyle() As String
        Get
            Return _ActiveDimStyle
        End Get
        Set(ByVal value As String)
            _ActiveDimStyle = value
        End Set
    End Property

    Private _ActiveTextStyle As String
    Public Property ActiveTextStyle() As String
        Get
            Return _ActiveTextStyle
        End Get
        Set(ByVal value As String)
            _ActiveTextStyle = value
        End Set
    End Property


    Private _app As ClsAcadApplication
    Public Property Application() As ClsAcadApplication
        Get
            Return _app
        End Get
        Set(ByVal value As ClsAcadApplication)
            _app = value
        End Set
    End Property

    Public AcadObject As Object

    Public Sub GetInforFromAcad(acDoc As Object, app As ClsAcadApplication)
        If TypeName(acDoc) = "IAcadDocument" Then
            Me.AcadObject = acDoc
            Dim t = Me.GetType()
            For Each item As PropertyInfo In t.GetProperties().ToList().Where(Function(c) c.CanWrite).ToList()
                Dim val = CallByName(acDoc, item.Name, CallType.Get, Nothing)
                If item.PropertyType.Namespace <> MethodBase.GetCurrentMethod().ReflectedType.Namespace Then
                    If val.GetType().IsCOMObject Then
                        item.SetValue(Me, val.Name, Nothing)
                    Else
                        item.SetValue(Me, val, Nothing)
                    End If

                Else
                    Me.Application = app
                End If
            Next
        End If
    End Sub

    Public Sub Activate()
        Me.AcadObject.Activate()
    End Sub

    ''' <summary>
    '''True: AutoCAD should attempt to fix any problems it encounters. False: AutoCAD should Not attempt To fix any problems it encounters. 
    ''' </summary>
    ''' <param name="FixError"></param>
    Public Sub AuditInfo(FixError As Boolean)
        Me.AcadObject.AuditInfo(FixError)
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="Name">The name of the variable to return. Must contain a valid system variable name (the case of the characters is not significant).</param>
    ''' <returns></returns>
    Function GetVariable(Name As String) As Object
        Return Me.AcadObject.GetVariable(Name)
    End Function

    Public Sub PurgeAll()
        Me.AcadObject.PurgeAll()
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="Name">The name of the system variable to set. </param>
    ''' <param name="Value">The new value for the specified system variable.</param>
    Public Sub SetVariable(Name As String, Value As Object)
        Me.AcadObject.SetVariable(Name, Value)
    End Sub

    ''' <summary>
    ''' Regenerates the entire drawing and recomputes the screen coordinates and view resolution for all objects. 
    ''' </summary>
    ''' <param name="WhichViewports"></param>
    Public Sub Regen(WhichViewports As AcRegenType)
        Me.AcadObject.Regen(WhichViewports)
    End Sub

    ''' <summary>
    ''' Saves the document to a specified file; no longer supported for menu groups
    ''' </summary>
    ''' <param name="FileName">The full path and file name, or valid URL address, for the file. The active document takes on the new name. </param>
    ''' <param name="FileType">AcSaveAsType enum </param>
    Public Sub SaveAs(FileName As String, FileType As AcSaveAsType)
        Me.AcadObject.Save(FileName, FileType)
    End Sub

    Public Function GetBlocksCollection() As ClsAcadBlock()
        Dim acadBlks = Me.AcadObject.Blocks
        Dim blks As New List(Of ClsAcadBlock)
        For index = 0 To acadBlks.Count - 1
            Dim blk As New ClsAcadBlock, blkcad As Object
            blkcad = acadBlks.Item(index)
            If blkcad IsNot Nothing Then
                blk.CteateFromCad(blkcad, Me, Me.Application)
                blks.Add(blk)
            End If
        Next
        Return blks.ToArray()

    End Function
    Public Enum AcRegenType
        'Regenerates only the active viewport. 
        acActiveViewport = 0
        'Regenerates all viewports on the document.
        acAllViewports = 1
    End Enum

    Public Enum AcSaveAsType
        ac2000_dwg = 12 'AutoCAD 2000 DWG (*.dwg) 
        ac2000_dxf = 13 'AutoCAD 2000 DXF (*.dxf) 
        ac2004_dwg = 24 'AutoCAD 2004 DWG (*.dwg) 
        ac2004_dxf = 25 'AutoCAD 2004 DXF (*.dxf) 
        ac2007_dwg = 36 'AutoCAD 2007 DWG (*.dwg) 
        ac2007_dxf = 37 'AutoCAD 2007 DXF (*.dxf) 
        ac2010_dwg = 48 'AutoCAD 2010 DWG (*.dwg) 
        ac2010_dxf = 49 'AutoCAD 2010 DXF (*.dxf) 
        ac2013_dwg = 60 'AutoCAD 2013 DWG (*.dwg) 
        ac2013_dxf = 61 'AutoCAD 2013 DXF (*.dxf) 
        ac2018_dwg = 64 'AutoCAD 2018 DWG (*.dwg) 
        ac2018_dxf = 65 'AutoCAD 2018 DXF (*.dxf) 
    End Enum
End Class


